04. Exercise: Tour of the App
L5 03 Tour Of The App HS-SC
Download the Code for this Lesson
You have two options for the code in this lesson:
- You can download a zip file of the starter code here
- If you’re familiar with git, you can clone the entire repository. This includes
//TODOinstructions and solution states for every step. To work with the git repository, refer to the code README.
Starter code Walkthrough
MainActivity- This class does very little. It’s just a container for the NavHostFragment. The fragments that go in the NavHostFragment do most of the heavy lifting. This is similar to what we did in lesson 3res/navigation/main_navigation.xml- This is the navigation graph for this app. The navigation flow goes from the TitleFragment to the GameFragment to the ScoreFragment. From the ScoreFragment, you can play the game again by going back to the GameFragment. Note that the action from the GameFragment to the ScoreFragment has a Pop To attribute that removes the game from the backstack. This makes it so that you can never press the back button from the ScoreFragment to go back to a finished game. Instead you’ll go to the title screen.TitleFragment- This is a simple fragment showing the title screen. Has a single button that takes you to the GameFragment.GameFragment- This fragment contains all the logic for the GuessIt game itself. It contains:word- A variable for the current word to guess.score- A variable for the current score.wordList- A variable for a mutable list of all the words you need to guess. This list is created and immediately shuffled usingresetList()so that you get a new order of words every time.resetList()- Method that creates and shuffles the list of words.onSkip()/onCorrect()- Methods for when you press the Skip/Got It buttons. They modify the score and then go to the next word in yourworldList.nextWord()- A method for moving to the next word to guess. If there are still words in your mutable list of words, remove the current word, and then setcurrentWordto whatever is next in the list. This will finish the game if there are no more words to guess.gameFinished()- A method that is called to finish the game. This passes your current score to the ScoreFragment using SafeArgs.
ScoreFragment- This fragment gets the score passed in from the argument bundle and displays it. There’s also a button for playing again, that takes you back to the GameFragment.